package com.nms.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.nms.dao.EmailDao;
import com.nms.model.Email;

@Component("emailDao")
public class EmailDaoImpl implements EmailDao {
	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Override
	public boolean save(Email e) {
		boolean flag = false;
		try {
			hibernateTemplate.save(e);
			flag = true;
		} catch (Exception ex) {
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean delete(int eid) {
		boolean flag = false;
		try {
			Session session = hibernateTemplate.getSessionFactory()
					.openSession();
			String hql = "delete from Email e where e.id= " + eid;
			Query q = session.createQuery(hql);
			q.executeUpdate();
			flag = true;
		} catch (Exception ex) {
			flag = false;
		}
		return flag;
	}

	@Override
	public Email getEmail(int eid) {
		Email e = (Email) hibernateTemplate.get(Email.class, eid);
		return e;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Email> findUEmail(String username) {
		List<Email> list = hibernateTemplate
				.find("from Email where tousername = '" + username + "'");
		return list;
	}
}
